import Taro from "@tarojs/taro";

// const baseUrl = "http://localhost:8081/sinpo"
// const baseUrl = "http://124.222.22.55:8087/sinpo"
const baseUrl = "https://www.tuanlianbao.com/api"

export function request(url: string, method, data: any, callback) {
  const token = Taro.getStorageSync('token');
  Taro.request({
    url: baseUrl + url,
    data,
    header: {
      'Authorization': 'Bearer ' + token,
    },
    method,
    success: (result: any) => {
      callback(result.data)
    }
  })
}

export function getRequest(url: string, callback) {
  request(url, 'GET', null, callback);
}

export function postRequest(url: string, data, callback) {
  request(url, 'POST', data, callback);
}

export function putRequest(url: string, data, callback) {
  request(url, 'PUT', data, callback);
}

export function deleteRequest(url: string, callback) {
  request(url, 'DELETE', null, callback);
}

export function uploadFile(filePath: string, fileType: number, callback) {
  Taro.uploadFile({
    url: baseUrl + `/upload/file?fileType=${fileType}`,
    filePath: filePath,
    name: 'file',
    success: (result: any) => {
      callback(result)
    }
  })
}
